package com.softer.chengxin.home.service.impl;

import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.home.dao.HomeDao;
import com.softer.chengxin.home.dao.StreetDao;
import com.softer.chengxin.home.domin.Street;
import com.softer.chengxin.home.service.StreetService;
import com.softer.chengxin.sysrole.entity.ModuleStreet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/10/18
 */
@Service
public class StreetServiceImpl implements StreetService {
    @Autowired
    private StreetDao streetDao;
    @Autowired
    private HomeDao homeDao;

    /**
     * 加载街道列表
     *
     * @return 列表
     */
    @Override
    public List<Street> loadStreet() {
        List<Street> list = streetDao.getStreet(Common.ZERO);
            if (list != null) {
                for (Street street : list) {
                    List<Street> children = streetDao.getStreet(street.getId());
                    street.setChildren(children);
                    if(null != children && children.size()>0){
                        for (Street s : children) {
                            List<Street> c = streetDao.getStreet(s.getId());
                            if(null != c && c.size()>0){
                                s.setChildren(c);
                            }
                        }
                    }
                }
                return list;
        }
        return new ArrayList<>();
    }

    /**
     * 根据登陆用户，选择街道列表
     *
     * @param id 登陆账号
     * @return 列表
     */
    @Override
    public List<Street> loadStreetByUser(Integer id) {
        //查询管理区域
        String area = homeDao.findAreaId(id);
        List<Street> list =  streetDao.getStreetByUser(Common.ZERO,area);
        if (list != null) {
            for (Street street : list) {
                List<Street> children = streetDao.getStreetByUser(street.getId(),area);
                street.setChildren(children);
                if(null != children && children.size()>0){
                    for (Street s : children) {
                        List<Street> c = streetDao.getStreet(s.getId());
                        s.setChildren(c);
                    }
                }
            }
            return list;
        }
        return new ArrayList<>();
    }
}
